<template>
	<view>
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">按月统计</block>
		</cu-custom>

		<view>
			<view class="cu-bar bg-white">
				<view class="action">
					<text class="cuIcon-title text-blue"></text>选择月份
				</view>
				<picker fields="month" mode="date" :end="enddate" :value="date" @change="bindDateChange">
					<view class="action">
						<text class="padding">{{date}}</text><text class="cuIcon-unfold"></text>
					</view>
					
				</picker>
			</view>
			<view class="cu-bar">
				<view class="action">
					总人数{{statistics.persum}}人
				</view>
			</view>
			<view @click="goDetail(item)" v-for="item in statistics.statistics" class="cu-bar bg-white solid-bottom">
				<view class="action">{{item.title}}</view>
				<view class="action">{{item.persum}}人</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		formateDate
	} from "@/common/utils.js"
	let curDate = new Date() //获取当前日期
	export default {
		data() {
			return {
				date: formateDate(curDate, "Y-M"),
				statistics: {},
				enddate: formateDate(curDate, "Y-M")
			}
		},
		onLoad() {
			this.getData()
		},
		methods: {
			// 选择打卡日期
			bindDateChange: function(e) {
				let time = e.target.value
				var selDate = new Date(Date.parse(time.replace(/-/g, "/")));
				if (selDate > curDate) {
					uni.showToast({
						title: '日期不能超过本月',
						icon: 'none'
					})
				}
				//如果选择当前日期，不做操作
				else if (time != this.date) {
					this.date = time
					this.getData()
				}
			},
			goDetail(item) {
				if (item.persum == 0 || item.persum == '') {
					uni.showToast({
						title: '没有数据哦',
						icon: 'none'
					})
					return
				}
				item.date = this.date //加入选择的日期
				uni.navigateTo({
					url: `../monthly-statistics-person/monthly-statistics-person?data=${JSON.stringify(item)}`
				})
			},
			getData() {
				uni.showLoading({
					title:'加载中'
				})
				uni.request({
					url: this.$constant.BASEURL + this.$constant.GET_MONTHY_STATISTICS,
					method: "POST",
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						username: getApp().globalData.username,
						token: getApp().globalData.token,
						orgcode: getApp().globalData.orgcode,
						ymonth: this.date
					},
					complete() {
						uni.hideLoading()
					},
					success: (res) => {
						//跳转到登陆页面
						if (res.data.code == 0) {
							let data = res.data.data
							console.log(data)
							if (typeof(data) == 'undefined' || data == null) return
							let statistics = {
								persum: data.persum,
								statistics: [{
										id: 'attcount',
										persum: data.attcount,
										title: '出勤',
									},
									{
										id: 'late',
										persum: data.late,
										title: '迟到'
									},
									{
										id: 'leaveearly',
										persum: data.leaveearly,
										title: '早退'
									},
									{
										id: 'absenteeism',
										persum: data.absenteeism,
										title: '旷工'
									},
									{
										id: 'leavhourss',
										persum: data.leavesnum,
										title: '请假'
									},
								]
							}
							this.statistics = statistics
						} else {
							uni.showToast({
								title: res.data.message,
								icon: 'none'
							})
						}
					}
				});
			}
		}
	}
</script>

<style>

</style>
